package com.rt.cms.controller;

import com.rt.cms.common.Constants;
import com.rt.cms.common.base.Page;
import com.rt.cms.common.base.PageResultSet;
import com.rt.cms.common.base.Result;
import com.rt.cms.common.utils.SendMessageUtil;
import com.rt.cms.data.dto.company.ArchivesDto;
import com.rt.cms.data.entity.CompanyInfoBean;
import com.rt.cms.data.entity.TbApprovalRecord;
import com.rt.cms.data.entity.TbCompanyInfo;
import com.rt.cms.data.entity.system.User;
import com.rt.cms.service.ArchivesService;
import com.rt.cms.service.CompanyInfoService;
import com.rt.cms.service.system.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create with IDEA
 *
 * @author Wangzs
 * @Description
 * @Date 2018/6/6
 * @Time 15:59
 */
@Controller
@RequestMapping("fileCheck")
public class FileCheckController {

    @Autowired
    private ArchivesService archivesService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyInfoService companyInfoService;

    @RequestMapping(method = RequestMethod.GET)
    public String toPage(Model model) {
        return "archives/fileCheckList";
    }

    /**
     * 档案库部门审核展示页面
     *
     * @param page
     * @return
     */
    @ResponseBody
    @RequiresPermissions("check:view")
    @RequestMapping("/queryByStatusDepartCheck")
    public PageResultSet<ArchivesDto> queryByStatusDepartCheck(Page page, HttpServletRequest request, String comName,String fileStatus) {
        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        List<ArchivesDto> dtoList = new ArrayList<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(0);
        Integer status = Constants.Audit_Of_Department;
        String dormant = String.valueOf(Constants.WaitDormancy);
        User user = (User) request.getAttribute(Constants.CURRENT_USER);
        int currentId = user.getId().intValue();
        List<User> userlist = userService.selectAllUserBypId(currentId);
        String ids = "";
        for (int i = 0; i < userlist.size(); i++) {
            if (i == 0) {
                ids = userlist.get(i).getId().toString();
            } else {
                ids = ids + "," + userlist.get(i).getId();
            }
        }
        if ("".equals(ids)) {
            return resultSet;
        }
        return archivesService.findByStatus(status, page, ids, comName, currentId, dormant,fileStatus);
    }

    /**
     * 档案审核操作
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequiresPermissions("check:view")
    @RequestMapping(value = "/checkStatus", method = RequestMethod.POST)
    public Result queryByStatusDepartCheck(TbCompanyInfo tbCompanyInfo, HttpServletRequest request) {
        //更新状态
        int operateType = tbCompanyInfo.getOperateType();
        String remark = tbCompanyInfo.getRemark();
        //记录操作记录
        User user = (User) request.getAttribute(Constants.CURRENT_USER);
        Date now = new Date();
        tbCompanyInfo.setCheckOperateId(user.getId().intValue());

        TbApprovalRecord tbApprovalRecord = new TbApprovalRecord();
        tbApprovalRecord.setOperateId(user.getId().intValue());
        tbApprovalRecord.setOperateTime(now);
        tbApprovalRecord.setDemo(remark);
        //审核通过
        if (Constants.Agree_Of_Department == Integer.valueOf(operateType)) {
            tbCompanyInfo.setStatus(Constants.Audit_Of_Wind);
            tbApprovalRecord.setOperateType(Constants.Agree_Of_Department);
            tbApprovalRecord.setStatus(Constants.Audit_Of_Wind);

            // 给风控发短信
            String phone = userService.queryRiskPhoneByRole();
            SendMessageUtil.postUrl(Constants.Sms_DepartmentAudit2RiskAudit, Constants.Sms_URL, phone);
        } else if (Constants.Refuse_Of_Department == Integer.valueOf(operateType)) {
            //审核拒绝
            tbCompanyInfo.setStatus(Constants.Due_Diligence);
            tbApprovalRecord.setOperateType(Constants.Refuse_Of_Department);
            tbApprovalRecord.setStatus(Constants.Due_Diligence);
            tbApprovalRecord.setIsRead(0);

            // 给A发短信
            String phone = userService.queryPhoneById(companyInfoService.getCompanyInfoById(tbCompanyInfo.getId()).getOperateId());
            SendMessageUtil.postUrl(Constants.Sms_DepartmentOrRiskRefuse, Constants.Sms_URL,phone);
        } else if (Constants.AgreeInDormancy == Integer.valueOf(operateType)) {
            //休眠通过
            tbCompanyInfo.setDormant(Constants.Dormancy);
            tbApprovalRecord.setOperateType(Constants.AgreeInDormancy);
            tbApprovalRecord.setStatus(Constants.Dormancy);

            // 给A发短信
            String phone = userService.queryPhoneById(companyInfoService.getCompanyInfoById(tbCompanyInfo.getId()).getOperateId());
            SendMessageUtil.postUrl(Constants.Sms_AgreeInDormancy, Constants.Sms_URL, phone);
        } else if (Constants.RefuseInDormancy == Integer.valueOf(operateType)) {
            //休眠拒绝
            tbCompanyInfo.setDormant(Constants.unDormancy);
            tbApprovalRecord.setOperateType(Constants.RefuseInDormancy);
            tbApprovalRecord.setStatus(Constants.unDormancy);
            tbApprovalRecord.setDemo(remark);
            tbApprovalRecord.setIsRead(0);

            // 给A发短信
            String phone = userService.queryPhoneById(companyInfoService.getCompanyInfoById(tbCompanyInfo.getId()).getOperateId());
            SendMessageUtil.postUrl(Constants.Sms_RefuseInDormancy, Constants.Sms_URL, phone);
        } /*else if (Constants.AgreeOutDormancy == Integer.valueOf(operateType)) {
            //取消休眠通过
            tbCompanyInfo.setDormant(Constants.unDormancy);
            tbApprovalRecord.setOperateType(Constants.AgreeOutDormancy);
            tbApprovalRecord.setStatus(Constants.unDormancy);
        } else if (Constants.RefuseOutDormancy == Integer.valueOf(operateType)) {
            //取消休眠拒绝
            tbCompanyInfo.setDormant(Constants.Dormancy);
            tbApprovalRecord.setOperateType(Constants.RefuseOutDormancy);
            tbApprovalRecord.setStatus(Constants.Dormancy);
            tbApprovalRecord.setDemo(remark);
            tbApprovalRecord.setIsRead(0);
        } */ else if (Constants.OutDormancy == Integer.valueOf(operateType)) {
            //申请取消休眠
            tbCompanyInfo.setDormant(Constants.unDormancy);
            tbApprovalRecord.setOperateType(Constants.OutDormancy);
            tbApprovalRecord.setStatus(Constants.unDormancy);
            tbApprovalRecord.setDemo(remark);
            //tbApprovalRecord.setIsRead(0);
        } else if (Constants.InDormancy == Integer.valueOf(operateType)) {
            //申请休眠
            tbCompanyInfo.setDormant(Constants.WaitDormancy);
            tbApprovalRecord.setOperateType(Constants.InDormancy);
            tbApprovalRecord.setStatus(Constants.WaitDormancy);
            tbApprovalRecord.setDemo(remark);

        }

        //修改企业信息
        if (companyInfoService.editCompanyInfo(tbCompanyInfo, tbApprovalRecord) > 0) {
            //map.put("message", "修改企业信息成功");
        } else {
            //map.put("message", "修改企业信息失败");
        }


        System.out.print(tbCompanyInfo.getId());
        return Result.Success();
    }

    /**
     * 档案审核回显
     *
     * @param
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("check:view")
    public String detail(int id, Model model) {

        CompanyInfoBean companyInfoBean = companyInfoService.getCompanyInfoBeanById(id);
        CompanyInfoBean guaranteeCompanyInfoBean = new CompanyInfoBean();
        List<CompanyInfoBean> companyInfoBeans = companyInfoService.getGuaranteeCompanyInfoBean(id);
        /*if (companyInfoBeans.size() > 0) {
            guaranteeCompanyInfoBean = companyInfoBeans.get(0);
        }*/
        model.addAttribute("archives", companyInfoBean);

        model.addAttribute("archivesGuarantees", companyInfoBeans);
        return "archives/fileCheckDetail";
    }

}
